package com.uwlaser.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.domain.DataMaterialSingle;
import com.uwlaser.service.DataMaterialSingleService;
import com.uwlaser.service.dto.DataMaterialSingleDto;
import com.uwlaser.service.dto.DataMaterialSingleQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "物料单件码管理")
@RequestMapping("/api/dataMaterialSingle")
public class DataMaterialSingleController {

    private final DataMaterialSingleService dataMaterialSingleService;

    @GetMapping
    @Log("查询物料单件码")
    @ApiOperation("查询物料单件码")
    @PreAuthorize("@el.check('dataMaterialSingle:list')")
    public ResponseEntity query(DataMaterialSingleQueryParam query, Pageable pageable){
        return new ResponseEntity<>(dataMaterialSingleService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增物料单件码")
    @ApiOperation("新增物料单件码")
    @PreAuthorize("@el.check('dataMaterialSingle:add')")
    public ResponseEntity create(@Validated @RequestBody DataMaterialSingleDto resources){
        return new ResponseEntity<>(dataMaterialSingleService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改物料单件码")
    @ApiOperation("修改物料单件码")
    @PreAuthorize("@el.check('dataMaterialSingle:edit')")
    public ResponseEntity update(@Validated @RequestBody DataMaterialSingleDto resources){
        dataMaterialSingleService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除物料单件码")
    @ApiOperation("删除物料单件码")
    @PreAuthorize("@el.check('dataMaterialSingle:del')")
    public ResponseEntity delete(@RequestBody Set<String> ids) {
        dataMaterialSingleService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @Log("导出物料单件码")
    @ApiOperation("导出物料单件码")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('dataMaterialSingle:list')")
    public void download(HttpServletResponse response, DataMaterialSingleQueryParam query) throws IOException {
        dataMaterialSingleService.download(dataMaterialSingleService.queryAll(query), response);
    }

}
